// pages/filter/index.js
import load from '../templates/loader/loader.js'
import api from '../../api/api.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    arrayData: [],
    arrayDataChild: [],
    modal: {
      title: "",
      open: "",
      hasTitle: false,
      panelType: 0
    },
    panelType: ["surfaceColor", "shadeColor"],
    sbData: [],
    currentSelect: [], //当前选择
    filterArray:[],
    selectArray: [],
    shapeView: {
      title: "标题",
      active: false,
      item: [],
      child: []
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    wx.removeStorage({
      key: 'selected'
    })
    wx.setNavigationBarTitle({
      title: options.title
    })
    //初始化载入等待动画
    that.loader = load.loader(that, {
      data: "loader",
      toast: true,
      size: 50,
      borderWidth: 3
    });
    that.setData({
      id: options.id
    })
    that.loader.open(); //打开载入等待
    that.loadData(options);
  },
  onShow:function(){
    var that=this;
    wx.getStorage({
      key: 'selected',
      success: function(res) {
        that.data.selectArray = []
        that.data.arrayData.forEach(function (item) {
          var selectData = []
          item.select = "";
          res.data.forEach(function (item2) {
            if (item.id == item2.pid) {
              selectData.push(item2.attribute_name)
              item.select = selectData.join("、")
              that.data.selectArray.push(item2.id)
            }
          })
        })
        that.setData({
          arrayData: that.data.arrayData
        })
      },
    })
  },
  loadData: function (options) {
    var that = this;
    var jsonStr = '{"REQ_HEAD":{"SIGN":"null"},"REQ_BODY":{"classifyId":"' + options.id + '"}}';
    api._filterIndex({
      method: 'POST',
      data: {
        REQ_MESSAGE: jsonStr
      },
      success: (res) => {
        that.loader.close(); //关闭载入动画
        if (JSON.stringify(res.rows) == "[]" || !res.rows) {
          that.loader.open("没有更多数据了"); //打开载入等待
          return;
        }
        //把“其他”排序到最后面
        var other = {};
        res.rows.forEach(function (item, index) {
          if (item.attribute_name == "其他" && item.parent_id == "1") {
            console.log(item.attribute_name)
            other = item;
            res.rows.splice(index, 1)
          }
        })
        res.rows.push(other)
        that.setData({
          arrayData: res.rows
        })

      },
      fail: (res) => {
        that.loader.open(api.statusFail.title);
      }
    });
  },
  selectType: function (data, callBack) {
    var that = this;
    var fiter = that.fiterItem(data.id);
    console.log(fiter)
    console.log(data.id)
    if (fiter.length <= 0) {
      wx.showModal({
        title: '提示',
        showCancel: false,
        confirmColor: "#e30c26",
        content: '没有选项'
      })
      return
    }
    that.setData({
      arrayDataChild: fiter
    })
    if (typeof callBack == 'function')
      callBack()
  },
  //提交筛选
  submit_fiter: function () {
    var that = this
    var array = that.data.selectArray.sort()
    console.log(array)
    var data = JSON.stringify(array)
    if (data == '[]' || !data) {
      wx.showModal({
        title: '提示',
        showCancel: false,
        confirmColor: "#e30c26",
        content: '至少选择一项，选择参数越多，匹配结果越精准。您确定已选完了吗?'
      })
    } else {
      var str = encodeURI(encodeURI(data));
      wx.redirectTo({
        url: '/pages/goods/goodslist/goodslist?keyWord=' + str + '&species_id='+that.data.id,
      })
    }
  },
  optionSwitch:function(e){
    var that = this
    var slefData = e.currentTarget.dataset
    console.log(slefData)
    that.data.filterArray=[];
    that.getFilterData(slefData.id);
    var data = JSON.stringify(that.data.filterArray)
    wx.navigateTo({
      url: '/pages/filter/secondary?id=' + that.data.arrayData[slefData.index].id+'&title=' + that.data.arrayData[slefData.index].attribute_name+'&data=' + data
    })
  },
  getFilterData:function(id){
    var that=this;
    that.data.arrayData.forEach(function (item) {
      if (item.parent_id == id) {
        that.data.filterArray.push(item);
        that.getFilterData(item.id)
      }
    })
  },
  open_modal: function (e) {
    var that = this
    var slefData = e.currentTarget.dataset
    var modal = that.data.modal
    modal.open = "active"
    modal.title = slefData.title
    modal.panelType = 0
    if (that.data.panelType.indexOf(slefData.name) != -1) {
      modal.panelType = 1
    }
    that.selectType(slefData, function () {
      that.setData({
        modal: modal
      })
    })
  },
  close_modal: function () {
    var that = this;
    that.setData({
      modal: {
        open: "",
        panelType: 0
      }
    })
  },
  /**
   * 选择项
   */
  item_spec_tap: function (e) {
    var that = this
    var slefData = e.currentTarget.dataset
    //var arrayDataChild = that.data.arrayData[that.data.pid].child[that.data.index]
    that.data.currentSelect.forEach(function (item, index) {
      if (slefData.id == item.id) {
        that.data.currentSelect.splice(index, 1)
      }
    })

    that.data.arrayDataChild.forEach(function (item, index) {
      if (slefData.id == item.id) {
        item.active = item.active ? false : true
        if (item.active) {
          that.data.currentSelect.push(item);
        }
        //arrayDataChild.select = child.active ? child.text : ""
      } else {
        item.active = false
        that.data.currentSelect.forEach(function (select, index) {
          if (select.id == item.id) {
            that.data.currentSelect.splice(index, 1);
          }
        })
        //res.data.rows.splice(index, 1)
        //arrayDataChild.select = ""
      }
    })
    if (slefData.active == false) {
      that.data.shapeView.item = that.fiterItem(slefData.id)
      if (that.data.shapeView.item.length > 0) {
        that.data.shapeView.active = true;
        that.data.shapeView.title = slefData.text
        that.data.shapeView.item.forEach(function (item) {
          that.data.shapeView.child.push(that.fiterItem(item.id))
        })
      }
    }
    that.setData({
      arrayDataChild: that.data.arrayDataChild,
      shapeView: that.data.shapeView
    })
  },
  //确认选择
  confirm_select: function () {
    var that = this;
    var data = that.data.arrayData
    var currentSelect = that.data.currentSelect
    var selectData = []
    that.data.selectArray = []
    data.forEach(function (item) {
      item.select = "";
      currentSelect.forEach(function (select) {
        if (item.id == select.parent_id) {
          item.select = select.attribute_name
          that.data.selectArray.push(select.id)
        }
      })
    })

    that.setData({
      arrayData: data
    })
    that.close_modal()
  },
  //赛选数据
  fiterItem: function (id) {
    var that = this,
      obj = []
    that.data.arrayData.forEach(function (item) {
      item.active = false
      that.data.currentSelect.forEach(function (select) {
        if (select.id == item.id) {
          item.active = true
        }
      })
      if (item.parent_id == id) {
        obj.push(item)
      }
    })
    return obj
  },
  closeShape: function () {
    console.log("eee")
    var that = this;
    that.data.shapeView.active = false;
    that.setData({
      shapeView: that.data.shapeView
    })
  },
  shapeSelect: function (e) {
    var that = this;
    var slefData = e.currentTarget.dataset
    that.data.currentSelect.forEach(function (item, index) {
      if (slefData.id == item.id) {
        that.data.currentSelect.splice(index, 1)
      }
    })
    that.data.shapeView.child[slefData.pindex].forEach(function (item) {
      if (slefData.id == item.id) {
        item.active = item.active ? false : true
        if (item.active) {
          that.data.currentSelect.push(item);
        }
      } else {
        item.active = false;
        that.data.currentSelect.forEach(function (select, index) {
          if (select.id == item.id) {
            that.data.currentSelect.splice(index, 1);
          }
        })
      }
    });
    that.setData({
      shapeView: that.data.shapeView,
      currentSelect: that.data.currentSelect
    })
  }
})